<?php

//set style
$map_scr = 'https://maps.googleapis.com/maps/api/js?key='.$info->google_map_key->opt_val.'&v=3.exp&libraries=places';
$aJS     = array($map_scr, assets('js/fotorama.js', true), assets('js/map.js', true) );
$this->frontend_lib->setACss(assets('css/fotorama.css', true));
$this->frontend_lib->setAJs($aJS);

//check product state[sale, new, highlight]
$is_sale = false;
foreach ($product_sales as $index=>$ps) {
	if($ps->getId() == $product->product_id){
		$is_sale = true;
		unset($product_sales[$index]);
		break;
	}
}

//set main product
$startIndex = 0;
$selected_id = 0;
$selected_code = '';
$query_code = $this->input->get('code', true);
if($query_code!=false && trim($query_code)!=''){
    $selected_code = $query_code;
    foreach($product_detail as $index=>$item){
        if($item->getCode() == $selected_code){
            $startIndex = $index;
            $selected_id = $item->getId();
            break;
        }
    }
}
if($startIndex==0 && $selected_id==0) {
    foreach($product_detail as $index=>$item){
        if($item->getIsAvatar()==1){
            $startIndex = $index;
            $selected_id = $item->getId();
            $selected_code = $item->getCode();
            break;
        }
    }
}
?>
<!--product-details-->
<div class="product-details clear-mgr mgt10">
	<div class="col-md-5">
        <div id="product-img-slide-bg">
            <div class="fotorama" data-start-index='<?php echo $startIndex; ?>' id="product-img-slide">
                <?php foreach($product_detail as $item) { ?>
                <a href="<?php echo product_upload($item->image, $product->shop_image_folder, false); ?>">
                    <img src="<?php echo product_upload($item->image, $product->shop_image_folder); ?>" data-caption='<?php echo $item->getCode(); ?>'/>
                </a>
                <?php } ?>
            </div>       
        </div>

        <blockquote class="mgt5">
            <h5 class="contact-title">Thông tin người bán</h5>
            <a href="<?php echo shop_url($shop->getSlug()); ?>"><h3><?php echo $shop->getTitle(); ?></h3></a>
            <span class="block mgt10">
                <i class="fa fa-calendar-o"></i> <em>Tham gia từ : </em>
                <strong> <?php echo fdate_format($shop->getCreatedAt(), 'd/m/Y'); ?> </strong>
            </span>
            <span class="block mgt10">
                <i class="glyphicon glyphicon-home"></i>&nbsp;&nbsp;<?php echo $shop->address; ?>
                <br />
                <a data-toggle="modal" href="#modal-map" class="btn btn-primary"><i class="fa fa-map-marker"></i></i>&nbsp;Xem đuờng đi</a>
                <a href="#" data-toggle="tab" onclick="wireOfAction('#tab-info', 'click'); return false;" class="btn btn-primary"><i class="glyphicon glyphicon-new-window"></i>&nbsp;Thông tin thêm</a>
            </span>
        </blockquote>
    </div>

    <div class="col-sm-7 col-md-7">
        <form has-validate id="frm-product-detail" class="product-information" method="post" action="<?php echo page_url('cart/direct-push'); ?>">
            <input type="hidden" id="product-id" name="cart[id]" value="<?php echo $selected_id; ?>" />
            <input type="hidden" id="product-price" name="cart[price]" value="<?php echo $product->product_price_new; ?>" />
            
            <h2 id="product-in-set-name" class="item-title f20px clear-pad clear-mgr"><?php echo $product->product_title; ?></h2>

            <div class="detail_row_value" id="product-in-set-category">
                <a class="label label-danger w150" href="<?php echo category_url($product->cate_slug); ?>">
                    <i class="glyphicon glyphicon-tags"></i>&nbsp;
                    <span><?php echo $product->cate_title; ?></span>
                </a>
            </div>

            <div class="product-simple-detail_row  block">
                <span class="detail_row_value">
                    <span id="order-temp-price" class="normal-price-style"><?php echo currency_format($product->product_price_new); ?></span>
                    <?php if($is_sale) { ?>
                    <br/>
                    <i id="order-temp-price-old" class="price-old f20px"><?php echo currency_format($product->product_price_old); ?></i>
                    <?php } ?>
                </span>
            </div>

            <div class="product-simple-detail_row">
                <label class="detail_row_header" for="product-qty">Số lượng : </label>
                <div class="detail_row_value">
                    <input autocomplete="off" id="product-qty" name="cart[qty]" type="text" class="form-control w150 text-right" value="1" data-validate="required/numeric" data-validate-message='<?php echo $this->lang->line('error_quantity_invalid'); ?>' />
                </div>
            </div>

            <div class="product-simple-detail_row mgt10">
                <div class="detail_row_value">
                    <label for="product-temp-detail">Sản phẩm cùng loại: </label>
                    <select id="product-temp-detail" class="form-control w150">
                        <?php 
                        foreach($product_detail as $index=>$item) {
                            $seleted_option = "";
                            if($selected_code!=''){
                                if($item->getCode() == $selected_code) {
                                    $seleted_option = "selected";
                                }
                            } else {
                                if($item->getIsAvatar()==1) {
                                    $seleted_option = "selected";
                                }
                            }
                            $option_data = array(
                                'value'                 => $item->getId(),
                                'data-price'            => $item->getPriceNew(),
                                'data-price-old'        => $item->getPriceOld(),
                                'data-price-format'     => currency_format($item->getPriceNew()),
                                'data-price-old-format' => currency_format($item->getPriceOld()),
                                'data-index'            => $index,
                                'data-link'             => product_url($product->slug),
                                'selected'              => $seleted_option
                                );
                            $option_string = implode(' ', array_map(function ($v, $k) { return $k . "='" . $v."'"; }, $option_data, array_keys($option_data)));
                            ?>
                            <option <?php echo $option_string; ?> ><?php echo $item->getCode(); ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>

                <div id="flg_message" class="alert alert-success hidden mgt10"></div>

                <div class="product-simple-detail_row">
                    <button class="btn btn-primary" role="button">
                        <i class="fa fa-check-circle"></i>&nbsp;
                        Mua ngay
                    </button>
                    <button data-href="<?php echo page_url('cart/push'); ?>" id="btn-add-to-cart" type="button" class="btn btn-primary">
                        <i class="glyphicon glyphicon-shopping-cart"></i>&nbsp;
                        Cho vào giỏ hàng
                    </button>
                </div>
            </form>        

            <div id="social-share">
                <div class="fb-like" data-href="<?php echo product_url($product->product_slug); ?>" data-layout="box_count" data-action="like" data-show-faces="true"></div>
                <div class="g-plusone" data-href="<?php echo product_url($product->product_slug); ?>" data-size="tall"></div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!--/product-details-->

    <!--category-tab-->
    <div class="category-tab shop-details-tab">
       <div class="col-sm-12">
          <ul class="nav nav-tabs">
             <li class="active"><a href="#details" data-toggle="tab" id="tab-product-info">Chi tiết sản phẩm</a></li>
             <li><a href="#info" data-toggle="tab" id="tab-info">Thông tin Người bán</a></li>
             <li><a href="#companyprofile" data-toggle="tab">Phương thức thanh toán</a></li>
             <li><a href="#reviews" data-toggle="tab">Bình luận</a></li>
         </ul>
     </div>
     <div class="tab-content">
        <div class="tab-pane fade active in" id="details">
            <div class="col-xs-12 col-md-12 text-hyphens">
                <?php foreach($product_detail as $p){ ?>
                <?php
                $description_class = "";
                if($selected_code!=''){
                    if($p->getCode() != $selected_code) {
                        $description_class = "hidden";
                    }
                } else {
                    if($p->getIsAvatar()!=1) {
                        $description_class = "hidden";
                    }
                }
                ?>
                <div class="<?php echo $description_class; ?> product-description description-<?php echo $p->getId(); ?>">
                    <?php echo $p->getDescription(); ?>
                </div>
                <?php } ?>
            </div>
        </div>

        <div class="tab-pane fade" id="info">
            <div class="col-xs-12 col-md-12">
                <h2 class="contact-title">Thông tin người bán</h2>
                <a href="<?php echo shop_url($shop->getSlug()); ?>"><h3><?php echo $shop->getTitle(); ?></h3></a>
                <p class="mgt10">
                    <i class="fa fa-calendar-o"></i> <em>Tham gia từ : </em>
                    <strong> <?php $date = new DateTime($shop->getCreatedAt()); echo $date->format('d/m/Y'); ?> </strong>
                </p>
                <p>
                    <i class="glyphicon glyphicon-home"></i>&nbsp;&nbsp;<?php echo $shop->address; ?>
                </p>
                <p><i class="glyphicon glyphicon-phone-alt"></i>&nbsp;&nbsp;<?php echo $shop->phone; ?></p>
                <p><i class="glyphicon glyphicon-phone"></i>&nbsp;&nbsp;<?php echo $shop->home_phone; ?></p>
                <p>
                    <i class="glyphicon glyphicon-envelope"></i>&nbsp;&nbsp;
                    <a href="mailto:<?php echo $shop->email; ?>"><?php echo $shop->email; ?></a>
                </p>
                <p>
                    <i class="glyphicon glyphicon-globe"></i>&nbsp;&nbsp;
                    <a href="<?php echo $shop->getHomePage(); ?>"><?php echo getShopUrl($shop->getHomePage()); ?></a>
                </p>
            </div>
        </div>

        <div class="tab-pane fade" id="companyprofile">
            <div class="col-xs-12 col-md-12 text-hyphens">
                <?php echo $shop->getPaymentMethod(); ?>
            </div>
        </div>

        <div class="tab-pane fade clear-pad" id="reviews">
            <div class="col-xs-12 col-md-12">
                <div class="fb-comments" data-href="<?php echo product_url($product->product_slug); ?>" data-numposts="5" data-colorscheme="light"></div>
            </div>
        </div>
    </div>
</div>
<!--/category-tab-->
<?php $this->load->view('frontend/eshopper/items/map.html.php'); ?>